package com.zlsy.learning.leetcode.stack.s844;

import java.util.Stack;

/**
 * @author zhouliang
 * @date 2020/8/11 16:35
 *     <p>使用栈解决
 */
public class BackspaceCompare1 {

  public static boolean backspaceCompare(String S, String T) {
    return String.valueOf(build(S)).equals(String.valueOf(build(T)));
  }

  private static Stack<Character> build(String str) {
    Stack<Character> stack = new Stack<>();
    for (int i = 0; i < str.length(); i++) {
      char c = str.charAt(i);
      if (c == '#') {
        if (!stack.isEmpty()) {
          stack.pop();
        }
      } else {
        stack.push(c);
      }
    }
    return stack;
  }

  public static void main(String[] args) {
    String S = "a#c", T = "b";
    //    String S = "ab#c", T = "ad#c";
    boolean b = backspaceCompare(S, T);
    System.out.println(b);
  }
}
